package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.PayRuleDescriptionService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.PayRuleDescriptionDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing PayRuleDescription.
 */
@RestController
@RequestMapping("/api")
public class PayRuleDescriptionResource {

    private final Logger log = LoggerFactory.getLogger(PayRuleDescriptionResource.class);

    private static final String ENTITY_NAME = "payRuleDescription";

    private final PayRuleDescriptionService payRuleDescriptionService;

    public PayRuleDescriptionResource(PayRuleDescriptionService payRuleDescriptionService) {
        this.payRuleDescriptionService = payRuleDescriptionService;
    }

    /**
     * POST  /pay-rule-descriptions : Create a new payRuleDescription.
     *
     * @param payRuleDescriptionDTO the payRuleDescriptionDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new payRuleDescriptionDTO, or with status 400 (Bad Request) if the payRuleDescription has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/pay-rule-descriptions")
    @Timed
    public ResponseEntity<PayRuleDescriptionDTO> createPayRuleDescription(@RequestBody PayRuleDescriptionDTO payRuleDescriptionDTO) throws URISyntaxException {
        log.debug("REST request to save PayRuleDescription : {}", payRuleDescriptionDTO);
        if (payRuleDescriptionDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new payRuleDescription cannot already have an ID")).body(null);
        }
        PayRuleDescriptionDTO result = payRuleDescriptionService.save(payRuleDescriptionDTO);
        return ResponseEntity.created(new URI("/api/pay-rule-descriptions/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /pay-rule-descriptions : Updates an existing payRuleDescription.
     *
     * @param payRuleDescriptionDTO the payRuleDescriptionDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated payRuleDescriptionDTO,
     * or with status 400 (Bad Request) if the payRuleDescriptionDTO is not valid,
     * or with status 500 (Internal Server Error) if the payRuleDescriptionDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/pay-rule-descriptions")
    @Timed
    public ResponseEntity<PayRuleDescriptionDTO> updatePayRuleDescription(@RequestBody PayRuleDescriptionDTO payRuleDescriptionDTO) throws URISyntaxException {
        log.debug("REST request to update PayRuleDescription : {}", payRuleDescriptionDTO);
        if (payRuleDescriptionDTO.getId() == null) {
            return createPayRuleDescription(payRuleDescriptionDTO);
        }
        PayRuleDescriptionDTO result = payRuleDescriptionService.save(payRuleDescriptionDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, payRuleDescriptionDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /pay-rule-descriptions : get all the payRuleDescriptions.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of payRuleDescriptions in body
     */
    @GetMapping("/pay-rule-descriptions")
    @Timed
    public ResponseEntity<List<PayRuleDescriptionDTO>> getAllPayRuleDescriptions(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of PayRuleDescriptions");
        Page<PayRuleDescriptionDTO> page = payRuleDescriptionService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/pay-rule-descriptions");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /pay-rule-descriptions/:id : get the "id" payRuleDescription.
     *
     * @param id the id of the payRuleDescriptionDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the payRuleDescriptionDTO, or with status 404 (Not Found)
     */
    @GetMapping("/pay-rule-descriptions/{id}")
    @Timed
    public ResponseEntity<PayRuleDescriptionDTO> getPayRuleDescription(@PathVariable Long id) {
        log.debug("REST request to get PayRuleDescription : {}", id);
        PayRuleDescriptionDTO payRuleDescriptionDTO = payRuleDescriptionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(payRuleDescriptionDTO));
    }

    /**
     * DELETE  /pay-rule-descriptions/:id : delete the "id" payRuleDescription.
     *
     * @param id the id of the payRuleDescriptionDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/pay-rule-descriptions/{id}")
    @Timed
    public ResponseEntity<Void> deletePayRuleDescription(@PathVariable Long id) {
        log.debug("REST request to delete PayRuleDescription : {}", id);
        payRuleDescriptionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/pay-rule-descriptions?query=:query : search for the payRuleDescription corresponding
     * to the query.
     *
     * @param query the query of the payRuleDescription search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/pay-rule-descriptions")
    @Timed
    public ResponseEntity<List<PayRuleDescriptionDTO>> searchPayRuleDescriptions(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of PayRuleDescriptions for query {}", query);
        Page<PayRuleDescriptionDTO> page = payRuleDescriptionService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/pay-rule-descriptions");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
